package cn.pzhu.service.imp;

import java.util.regex.Pattern;

import cn.pzhu.pojo.Result;
import cn.pzhu.pojo.User;
import cn.pzhu.service.UserService;
import cn.pzhu.dao.UserDAO;
import cn.pzhu.dao.imp.UserDAOImp;

public class UserServiceImp implements UserService{

	private UserDAO userdao = new UserDAOImp();
	@Override
	public Result login(User user) {
		if(user==null || user.getUsername()==null || user.getPassword()==null) {
			return new Result(false,"接受前端数据异常");
		}
		if("".equals(user.getUsername())) {
			return new Result(false,"用户名不能为空");
		}
		if("".equals(user.getPassword())) {
			return new Result(false,"密码不能为空");
		}
//		if(user.getPassword().length()<6) {
//			return new Result(false,"密码长度不足6位");
//		}
		User dbuser = userdao.selectById(user.getUsername());
		if(dbuser==null) {
			return new Result(false,"此用户不存在");
		}
		if(dbuser.getStatus()==0) {
			return new Result(false,"此用户已被注销");
		}
		if(!dbuser.getPassword().equals(user.getPassword())) {
			return new Result(false,"密码输入错误");
		}
		return new Result(true,"登陆成功");
	}

	@Override
	public Result regist(User user) {
		if(user==null || user.getUsername()==null || user.getPassword()==null) {
			return new Result(false,"接受前端数据异常");
		}
		if("".equals(user.getUsername())) {
			return new Result(false,"用户名不能为空");
		}
		if("".equals(user.getPassword())) {
			return new Result(false,"密码不能为空");
		}
		if(user==null || user.getEmail()==null ||
				user.getGender()==null || user.getType()==null) {
			return new Result(false,"接受前端数据异常");
		}
		if("".equals(user.getEmail())) {
			return new Result(false,"邮箱不能为空");
		}
		String reg = "^[0-9a-zA-Z]{1,20}@[0-9a-zA-Z]{1,20}\\.[0-9a-zA-Z]{1,20}$";
		if(!Pattern.matches(reg, user.getEmail())) {
			return new Result(false,"邮箱格式错误");
		}
		if("null".equals(user.getType())) {
			return new Result(false,"没有选择关注类型");
		}
		if(userdao.selectById(user.getUsername())!=null) {
			return new Result(false,"此用户已经被注册");
		}
		int n = userdao.add(user);
		if(n==1) {
			return new Result(true,"注册成功");
		}else {
			return new Result(false, "注册失败");
		}
	}

	@Override
	public User showUserinfo(String username) {
		return userdao.selectById(username);
	}

	@Override
	public Result changeUserinfo(User user) {
		if(user==null || user.getUsername()==null) {
			return new Result(false,"接受前端数据异常");
		}
		if("".equals(user.getUsername())) {
			return new Result(false,"用户名不能为空");
		}
		if(user==null || user.getEmail()==null ||
				user.getGender()==null || user.getType()==null) {
			return new Result(false,"接受前端数据异常");
		}
		if("".equals(user.getEmail())) {
			return new Result(false,"邮箱不能为空");
		}
		String reg = "^[0-9a-zA-Z]{1,20}@[0-9a-zA-Z]{1,20}\\.[0-9a-zA-Z]{1,20}$";
		if(!Pattern.matches(reg, user.getEmail())) {
			return new Result(false,"邮箱格式错误");
		}
		if("null".equals(user.getType())) {
			return new Result(false,"没有选择关注类型");
		}
		User dbuser = userdao.selectById(user.getUsername());
		if(dbuser==null) {
			return new Result(false,"没有此用户");
		}
		user.setPassword(dbuser.getPassword());
		boolean b = userdao.update(user);
		if(b) {
			return new Result(true,"修改成功");
		}else {
			return new Result(false, "修改失败");
		}
	}

	@Override
	public Result changePassword(String username, String oldpassword, String newpassword) {
		if(username==null || oldpassword==null || newpassword==null) {
			return new Result(false,"接受前端数据异常");
		}
		if("".equals(username)) {
			return new Result(false,"用户名不能为空");
		}
		if("".equals(oldpassword)) {
			return new Result(false,"旧密码不能为空");
		}
		if("".equals(newpassword)) {
			return new Result(false,"新密码不能为空");
		}
		if(newpassword.length()<6) {
			return new Result(false,"密码长度不能小于6位");
		}
		User user = userdao.selectById(username);
		if(user==null) {
			return new Result(false,"该用户不存在");
		}
		if(newpassword.equals(oldpassword)) {
			return new Result(false,"新旧密码不能相同");
		}
		System.out.println(user.getPassword());
		System.out.println(oldpassword);
		if(!user.getPassword().equals(oldpassword)) {
			return new Result(false,"密码错误");
		}
		user.setPassword(newpassword);
		if(userdao.update(user)) {
			return new Result(true,"密码修改成功");
		}else {
			return new Result(false,"密码修改失败");
		}
	}

	@Override
	public boolean destroy(String username) {
		if(username==null || "".equals(username)) {
			return false;
		}
		User user = userdao.selectById(username);
		if(user==null)	return false;
		user.setStatus(0);
		userdao.update(user);
		return false;
	}
	
}
